package br.com.carser.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotEmpty;

import br.com.carser.dao.infra.BaseEntity;

/**
 * <B>Project: carser-web</B><BR>
 * 
 * Entity class that represents the Profile of the User.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Sep 18, 2011
 * @version 1.0
 */
@Entity
@Table(name = "tb_profile")
public class Profile extends BaseEntity implements Comparable<Profile> {

	/**
	 * Serial version ID.
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "id_profile", nullable = false, unique = true, updatable = true)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	/**
	 * Profile name.
	 */
	@Column(name = "cl_name", nullable = false, unique = true, updatable = true)
	@NotEmpty
	@NotNull
	@Pattern(regexp = "[a-zA-z]+", message="{validation.invalid.profile.name}")
	private String name;

	/**
	 * Permission list.
	 */
	@JoinTable(name = "profile_permission", 
			joinColumns = {@JoinColumn(name = "id_profile") }, 
			inverseJoinColumns = { @JoinColumn(name = "id_permission") })
	@ManyToMany(targetEntity = Permission.class)
	@Valid
	private List<Permission> permissions = new ArrayList<Permission>();

	@Override
	public Long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * get permissions
	 * 
	 * @return permission list
	 */
	public List<Permission> getPermissions() {
		return permissions;
	}

	/**
	 * Add a unique permission to list.
	 * 
	 * @param permission
	 */
	public void addPermission(final Permission permission) {
		permissions.add(permission);
	}

	/**
	 * Verify if profile contains permission.
	 * 
	 * @param permission
	 * @return true if contains.
	 */
	public boolean hasPermission(final Permission permission) {
		return permissions.contains(permission);
	}

	/**
	 * Verify if contains any of all permissions
	 * 
	 * @param permissions
	 * @return true if contain.
	 */
	public boolean hasAnyPermission(final List<Permission> permissions) {
		for (Permission permission : permissions) {
			if (this.permissions.contains(permission)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Verify if contain all of permissions.
	 * 
	 * @param permissions
	 * @return true if contains.
	 */
	public boolean hasAllPermissions(final List<Permission> permissions) {
		return this.permissions.containsAll(permissions);
	}

	/**
	 * Remove permission.
	 * 
	 * @param permission
	 * @return true if removed
	 */
	public boolean removePermission(final Permission permission) {
		return permissions.remove(permission);
	}

	/**
	 * remove all permissions.
	 */
	public void removeAllPermission() {
		permissions = new ArrayList<Permission>();
	}

	/**
	 * set permission list.
	 * 
	 * @param permissions
	 */
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Profile)) {
			return false;
		}
		Profile other = (Profile) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return name;
	}

	@Override
	public int compareTo(Profile o) {
		return this.name.compareTo(o.name);
	}

}
